package fule.com.webviewdemo.activity;

import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.text.Html;
import android.text.method.ScrollingMovementMethod;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.concurrent.ExecutionException;

import fule.com.webviewdemo.R;
import fule.com.webviewdemo.util.ScreenUtil;

/**
 * 作者： njb
 * 时间： 2018/7/10 0010-上午 10:29
 * 描述： TextView加载Img标签
 * 来源：
 */
public class TextViewForHtmlActivity extends AppCompatActivity {
    private TextView textView;
    private Drawable drawable;
    private String content;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_text_html);
        initView();
    }

    private void initView() {
        textView = findViewById(R.id.tv_detail);
        content = "<img src=\"https://www.38.hn/images/image/20161104/20161104144203_82117.jpg\" alt=\"\" /><img src=\"https://www.38.hn/images/image/20161116/20161116165842_88829.jpg\" alt=\"\" /><img src=\"https://www.38.hn/images/image/20161104/20161104144206_17003.jpg\" alt=\"\" /><img src=\"https://www.38.hn/images/image/20161104/20161104144207_62404.jpg\" alt=\"\" /><img src=\"https://www.38.hn/images/image/20161104/20161104144208_78881.jpg\" alt=\"\" />";
        setHtml(content);
    }

    /**
     * 设置html
     * @param content
     */
    private void setHtml(String content) {
        textView.setMovementMethod(ScrollingMovementMethod.getInstance());
        if (Build.VERSION.SDK_INT >= 24) {
            textView.setText(Html.fromHtml(content, Html.FROM_HTML_MODE_COMPACT, imageGetter, null));
        } else {
            textView.setText(Html.fromHtml(content, imageGetter, null));
        }
    }

    Html.ImageGetter imageGetter = new Html.ImageGetter() {
        @Override
        public Drawable getDrawable(String source) {
            if (drawable != null) {
                return drawable;
            } else {
                initDrawable(source);
            }
            return null;
        }
    };

    /**
     * 加载网络图片
     * @param s
     */
    private void initDrawable(final String s) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    final Drawable drawable1 = Glide.with(TextViewForHtmlActivity.this).load(s).submit().get();
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            if (drawable1 != null) {
                                drawable1.setBounds(0, 0, ScreenUtil.getScreenWidth(TextViewForHtmlActivity.this),ScreenUtil.getFactHeight(TextViewForHtmlActivity.this));
                                //多张图片情况下进行存储：drawableMap.put(s,drawable);
                                drawable = drawable1;
                                setHtml(content);
                            }
                        }
                    });
                } catch (InterruptedException | ExecutionException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
}
